package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.mapper.SysDeptMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PoliceVehicleBo;
import com.ruoyi.system.domain.vo.PoliceVehicleVo;
import com.ruoyi.system.domain.PoliceVehicle;
import com.ruoyi.system.mapper.PoliceVehicleMapper;
import com.ruoyi.system.service.IPoliceVehicleService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 警车档案Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-11
 */
@RequiredArgsConstructor
@Service
public class PoliceVehicleServiceImpl implements IPoliceVehicleService {

    private final PoliceVehicleMapper baseMapper;

    private final SysDeptMapper deptMapper;

    /**
     * 查询警车档案
     */
    @Override
    public PoliceVehicleVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询警车档案列表
     */
    @Override
    public TableDataInfo<PoliceVehicleVo> queryPageList(PoliceVehicleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PoliceVehicle> lqw = buildQueryWrapper(bo);
        Page<PoliceVehicleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);


        IPage<PoliceVehicleVo> convert = result.convert(k ->
        {
            Long managementUnit = k.getManagementUnit();
            SysDept sysDept = deptMapper.selectVoById(managementUnit);
            k.setManagementUnitName(sysDept.getDeptName());
            return k;
        });

        return TableDataInfo.build(convert);
    }

    /**
     * 查询警车档案列表
     */
    @Override
    public List<PoliceVehicleVo> queryList(PoliceVehicleBo bo) {
        LambdaQueryWrapper<PoliceVehicle> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PoliceVehicle> buildQueryWrapper(PoliceVehicleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoliceVehicle> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getManagementUnit()!= null, PoliceVehicle::getManagementUnit, bo.getManagementUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getPlateNumber()), PoliceVehicle::getPlateNumber, bo.getPlateNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getFrameNumber()), PoliceVehicle::getFrameNumber, bo.getFrameNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleType()), PoliceVehicle::getVehicleType, bo.getVehicleType());
        lqw.eq(StringUtils.isNotBlank(bo.getBrandModel()), PoliceVehicle::getBrandModel, bo.getBrandModel());
        lqw.eq(StringUtils.isNotBlank(bo.getVehiclePurpose()), PoliceVehicle::getVehiclePurpose, bo.getVehiclePurpose());
        lqw.eq(bo.getRegistrationDate() != null, PoliceVehicle::getRegistrationDate, bo.getRegistrationDate());
        return lqw;
    }

    /**
     * 新增警车档案
     */
    @Override
    public Boolean insertByBo(PoliceVehicleBo bo) {
        PoliceVehicle add = BeanUtil.toBean(bo, PoliceVehicle.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改警车档案
     */
    @Override
    public Boolean updateByBo(PoliceVehicleBo bo) {
        PoliceVehicle update = BeanUtil.toBean(bo, PoliceVehicle.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PoliceVehicle entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除警车档案
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
